<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      :append-to-body="true"
      align="center"
      class="base-dialog"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <div>
        <el-row>
          <el-col :span="4">
            <el-card shadow="never" style="height: 400px">
              <div style="height: 200px; overflow: auto">
                <el-row v-for="(item, index) in numberDependData" :key="index">
                  <el-checkbox
                    v-model="dependDataChangeState[index]"
                    @change="numberDependDataChange(index, item)"
                    >{{ item.name }}</el-checkbox
                  >
                </el-row>
              </div>
            </el-card>
          </el-col>
          <el-col :span="20">
            <el-card shadow="never" style="height: 400px">
              <div style="height: 380px; overflow: auto">
                <el-row v-for="(item, index) in showData" :key="index">
                  <el-row>
                    <el-col :span="4">{{ item.dependentObjectName }}</el-col>
                  </el-row>
                  <el-row
                    v-for="(items, indexs) in item.childrenData"
                    :key="indexs"
                  >
                    <el-col :span="8">{{ items.name }}</el-col>
                    <el-col :span="8">----------------</el-col>
                    <el-col :span="7">
                      <el-input v-model="items.content"></el-input>
                    </el-col>
                  </el-row>
                </el-row>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="base-dialog-footer">
        <el-button type="primary" @click="onSubmit">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getDictList } from "../../../api/System/SystemDict";
import { SampleCategoryParentGetList } from "../../../api/DetectionItemManage/SysBusinessSampleCategory";
export default {
  name: "ChoiceNumberDependDialog",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    dependData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      loading: false,
      nodData: [],
      keyarr: [],
      numberDependData: [],
      dependDataChangeState: [],
      showData: [],
    };
  },
  computed: {
    sysBusinessNumber() {
      return this.$t("SysBusinessNumber") || {};
    },
  },
  created() {
    this.showData = this.dependData;
    // 依赖对象
    getDictList({ dictCode: "NumberDepend" }).then((res) => {
      this.numberDependData = res.data;
      for (let i = 0; i < this.numberDependData.length; i++) {
        this.dependDataChangeState.push(false);
        for (let o = 0; o < this.showData.length; o++) {
          if (
            this.numberDependData[i].value ===
            String(this.showData[o].dependentObjectType)
          ) {
            this.dependDataChangeState[
              this.dependDataChangeState.length - 1
            ] = true;
          }
        }
      }
    });
  },
  methods: {
    // 复选框选中
    numberDependDataChange(index, data) {
      if (this.dependDataChangeState[index]) {
        if (data.value === "1") {
          // 查询委托领域
          getDictList({ dictCode: "EntrustDomain" }).then((res) => {
            var childrenData = [];
            for (let i = 0; i < res.data.length; i++) {
              childrenData.push({
                name: res.data[i].name,
                value: res.data[i].value,
                type: res.data[i].value,
                content: "",
              });
            }

            this.showData.push({
              dependentObjectType: data.value,
              dependentObjectName: data.name,
              childrenData: childrenData,
            });
          });
        } else if (data.value === "2") {
          // 查询顶级样品类别
          SampleCategoryParentGetList({}).then((res) => {
            var childrenData = [];
            for (let i = 0; i < res.data.length; i++) {
              childrenData.push({
                name: res.data[i].sampleCategoryName,
                value: res.data[i].id,
                type: res.data[i].value,
                foreignKey: res.data[i].id,
                content: "",
              });
            }
            this.showData.push({
              dependentObjectType: data.value,
              dependentObjectName: data.name,
              childrenData: childrenData,
            });
          });
        } else if (data.value === "3") {
          getDictList({ dictCode: "SamplingWayCode" }).then((res) => {
            var childrenData = [];
            for (let i = 0; i < res.data.length; i++) {
              childrenData.push({
                name: res.data[i].name,
                value: res.data[i].value,
                type: res.data[i].value,
                content: "",
              });
            }

            this.showData.push({
              dependentObjectType: data.value,
              dependentObjectName: data.name,
              childrenData: childrenData,
            });
          });
        }
      } else {
        for (let i = 0; i < this.showData.length; i++) {
          if (String(this.showData[i].dependentObjectType) === data.value) {
            this.showData.splice(i, 1);
          }
        }
      }
    },
    // 确定
    onSubmit() {
      // 判断是否为空,是否有重复
      var state = true;
      var contentArr = [];
      for (let i = 0; i < this.showData.length; i++) {
        for (let o = 0; o < this.showData[i].childrenData.length; o++) {
          if (
            this.showData[i].childrenData[o].content === null ||
            this.showData[i].childrenData[o].content === ""
          ) {
            state = false;
            break;
          }
          contentArr.push(this.showData[i].childrenData[o].content);
        }
        if (state === false) {
          break;
        }
      }
      if (!state) {
        this.$message.warning(
          this.$t("SysBusinessNumber.dependencyContentCannotEmpty")
        );
        return false;
      }

      // 判断内容是否重复
      var content = [...new Set(contentArr)];
      if (content.length !== contentArr.length) {
        this.$message.warning(
          this.$t("SysBusinessNumber.dependentContentCannotDuplicated")
        );
        return false;
      }

      for (let i = 0; i < this.showData.length; i++) {
        for (let o = 0; o < this.showData[i].childrenData.length; o++) {
          this.showData[i].childrenData[o].sort = i;
        }
      }

      this.$emit("choiceDependCloes", this.showData);
      this.handleClose();
    },
    handleClose() {
      this.$emit("update:dialogFormVisible", false);
    },
  },
};
</script>
